#Creates xml with values for sales data of products 
#and the quantity of sales in each quarter of a year
#by a combination of differing scales of two y-axes.
#The values required for building the xml is obtained as parameter arr_data
#It expects an array in which each element is 
#itself an array with first element as label, second element as sales value
#and third element as quantity value


xml = Builder::XmlMarkup.new

xml.map(:borderColor=>'FFFFFF', :connectorColor=>'000000', :fillAlpha=>'70', :hoverColor=>'FFFFFF', :showBevel=>'0') do 
  xml.colorRange() do 
    #TODO: do some dynamics
    ###########################################################################################
    ##
    ## This is for the dynamic coloring thing, not actual data
    ##
    ###########################################################################################
    
    # areacode_for_us => "areanumber(probably 3 digits)" => hash_data(areanumber, number of calls) (e.g. {213=>23, 805=>5657}) #total for one areanumber
    # (e.g. US-CA)    => "iso_country_code"              => string_data (must be US)
    #                 => "sum_calls"                     => integer_data(e.g. 2434)                ##total for one state
    temp_arr = []
    if arr_data != nil
      for item in arr_data
        temp_arr << Integer(item[1]["sum_calls"]) if item[0] != "UNKNOWN" #TODO: subject to change
      end
      
      if temp_arr != nil and temp_arr.size != 0 
        color_codes = ["FFFFCC", "FFFF99", "FFFF66", "FFFF33", "FFFF00", "FFCC99", "FF9933", "FF6666", "FF0066", "FF0033", "FF0000"]
        min = temp_arr.min
        max = temp_arr.max
        step = (max-min) / (color_codes.size-1)
        
        for i in 0...color_codes.size
          xml.color(:minValue=>Integer(step*i), \
                  :maxValue=>Integer(step*i + step), \
                  :displayValue=>Integer(step*i + step), \
                  :color=>color_codes[i])
        end
      else
        xml.color(:minValue=>'0', :maxValue=>'80', :displayValue=>'Poor', :color=>'CC0001')
        xml.color(:minValue=>'80', :maxValue=>'90', :displayValue=>'Average', :color=>'FFCC33')
        xml.color(:minValue=>'90', :maxValue=>'100', :displayValue=>'Good', :color=>'069F06')
        
      end
      
    end
  end
  ################################################################################################
  # areacode_for_us => "areanumber(probably 3 digits)" => hash_data(areanumber, number of calls) (e.g. {213=>23, 805=>5657}) #total for one areanumber
  # (e.g. US-CA)    => "iso_country_code"              => string_data (must be US)
  #                 => "sum_calls"                     => integer_data(e.g. 2434)                ##total for one state
  
  xml.data() do
    if arr_data != nil
      for item in arr_data
        key = item[0]; value=item[1]
        
        if key != "UNKNOWN"
          
          state = key
          areas = value["areanumber"]       # which is ARRAY !!
          iso   = value["iso_country_code"]   # string
          
          sum_calls= value["sum_calls"]     # number
          link = raw url_for(:action=>'state', :id=>state, :p=>params[:p], :from=>params[:from], :to=>params[:to], :iso=>iso)
          
          xml.entity(\
                  :id     => key.gsub("US-",""), \
                  :value  => value["sum_calls"], \
          #:link   => "javascript:onclick=\"detail_us('http://google.com',700);\"")
          #:link   => raw(link_to_function(key, "detail_us(\'#{link}\',700);")) \
          # <entity link="/fact_src_phone_numbers/state/US-TN?iso=US&amp;p=week&amp;from=&amp;to=" value="7431" id="TN"/>
                  :link =>"/#{controller.controller_name}/state/#{state}?iso=#{iso}&p=#{params["p"]}&from=#{params["from"]}&to=#{params["to"]}")  #exmaple: <entity id="AL" value="80.3" />
        end
      end
    end
  end
  
  xml.styles() do
    xml.definition() do
      xml.style(:type=>"animation", :name=>"animX", :param=>"_xscale", :start=>"0", :duration=>"1")
      xml.style(:type=>"animation", :name=>"animY", :param=>"_yscale", :start=>"0", :duration=>"1")
    end
    xml.application() do
      #xml.apply(:toObject=>"PLOT", :styles=>"animX,animY") 
      # This line has a problem. I don't know why
    end
  end
  
end